package com.hotelsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotelsystem.pojo.RoomType;
import com.hotelsystem.service.RoomTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/roomType")
public class RoomTypeController {

    @Resource
    private RoomTypeService roomTypeService;

    @RequestMapping("/toRoomType")
    public String roomType(RoomType roomType, Integer pageNum, Model model) {

        try {
            //设置当前页和每页数量
            PageHelper.startPage(pageNum,
                    5);
            //在这里看看是否有值
            System.out.println("是否有值"+roomType);
            //设置查询条件
             List<RoomType> roomTypeList = roomTypeService.getRoomTypesByRoomType(roomType);
            //获取PageInfo对象，该对象封装的分页的相关信息
            PageInfo<RoomType> pageInfo = new PageInfo<>(roomTypeList);
            //查询分页
            model.addAttribute("pageInfo",pageInfo);
            //数据回显
            System.out.println("roomType----"+roomType);
            model.addAttribute("roomType",roomType);
        } catch (Exception e) {
            e.printStackTrace();
            /*message = "查询房间状态信息时出现异常：" + e.getMessage();*/
        }
        return "room/typeRoomSet/roomType";
    }

    @ResponseBody
    @RequestMapping("/deleteRoomType")
    public Object deleteRoomType(Integer roomTypeId){
        Map<String,Object> maps = new HashMap<>();
        try {
            Integer result = roomTypeService.deleteRoomTypeBytypeId(roomTypeId);
            //Integer result = 0;
            if (result>0){
                maps.put("success","删除成功");
            }else {
                maps.put("success","当前类型下还有已预订和已入住房间,删除失败");
            }

        } catch (Exception e) {
            maps.put("success","删除失败");
            e.printStackTrace();
        }
        return maps;
    }

    @RequestMapping("/addRoomType")
    @ResponseBody
    public Object addRoomType(RoomType roomType){
        Map<String,Object> maps = new HashMap<>();
        try {
            roomTypeService.addRoomTypeByRoomType(roomType);
            maps.put("success",true);
        } catch (Exception e) {
            maps.put("success",false);
            e.printStackTrace();
        }
        return maps;
    }

    @RequestMapping("/updateRoomType")
    @ResponseBody
    public Object updateRoomType(RoomType roomType){
        Map<String,Object> maps = new HashMap<>();
        System.out.println("修改"+roomType);
        try {
            roomTypeService.updateRoomTypeByRoomType(roomType);
            maps.put("success",true);
        } catch (Exception e) {
            maps.put("success",false);
            e.printStackTrace();
        }
        return maps;
    }

    @ResponseBody
    @RequestMapping("/getRoomTypeByName")
    public Object getRoomTypeByName(String roomTypeName){
        try {
            return  roomTypeService.getRoomTypeBytypeName(roomTypeName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ResponseBody
    @RequestMapping("/getRoomTypeById")
    public Object getRoomTypeById(Integer roomTypeId){

        try {
            return roomTypeService.getRoomTypeInfoById(roomTypeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
